using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System;
using System.Collections.Generic;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_CommonEquipTips : UIBaseNode
    {
        public enum EquipTipBtnType
        {
            Detail = 0,
            Strengthen,
            Wear,
            TakeOff
        }


        #region Template Generate,don't modify
        protected partial class UIB_UIN_CommonEquipTips
        {
            #region ObjectBinding Generate
            public IQIGame.Onigao.Framework.ExArtSpriteNum bgQuality { protected set; get; }
            public UnityEngine.GameObject commonItem { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textName { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textNameEng { protected set; get; }
            public UnityEngine.GameObject attribueMould { protected set; get; }
            public UnityEngine.GameObject skillMould { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton buttonTakeOff { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton buttonWear { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton buttonDetail { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton buttonStrengthen { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExArtSpriteNum>("bgQuality", out var __tbv0);
                this.bgQuality = __tbv0;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("commonItem", out var __tbv1);
                this.commonItem = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textName", out var __tbv2);
                this.textName = __tbv2;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textNameEng", out var __tbv3);
                this.textNameEng = __tbv3;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("attribueMould", out var __tbv4);
                this.attribueMould = __tbv4;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("skillMould", out var __tbv5);
                this.skillMould = __tbv5;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("buttonTakeOff", out var __tbv6);
                this.buttonTakeOff = __tbv6;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("buttonWear", out var __tbv7);
                this.buttonWear = __tbv7;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("buttonDetail", out var __tbv8);
                this.buttonDetail = __tbv8;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("buttonStrengthen", out var __tbv9);
                this.buttonStrengthen = __tbv9;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields & properties
        private UIN_OneItem item = null;
        private ListScrollAdapter<UIN_CommonAttribute> attrs = null;
        private ListScrollAdapter<UIN_CommonEquipTipsSkill> skills = null;

        private HeroData heroData = null;
        private ItemEquipmentData equipItem = null;
        private ItemEquipmentData columnEquipItem = null;
        private System.Action<EquipTipBtnType, ItemEquipmentData> callOnClickBtn = null;
        private IReadOnlyDictionary<int, int> equipAllAttrCid2Value = null;
        private List<int> equipAllAttrCids = ListPool<int>.Get();

        protected UIB_UIN_CommonEquipTips ui { get; set; }
        #endregion

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_CommonEquipTips();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.item = this.InitChildNode<UIN_OneItem>(this.ui.commonItem);
            this.attrs = this.InitListScroll<UIN_CommonAttribute>(this.ui.attribueMould, this.OnAttrChanged);
            this.skills = this.InitListScroll<UIN_CommonEquipTipsSkill>(this.ui.skillMould, this.OnSkillChanged);

            this.ui.buttonDetail.onClick.AddListener(this.OnClickDetail);
            this.ui.buttonStrengthen.onClick.AddListener(this.OnClickStrengthen);
            this.ui.buttonTakeOff.onClick.AddListener(this.OnClickTakeOff);
            this.ui.buttonWear.onClick.AddListener(this.OnClickWear);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        protected override void OnDispose()
        {
            if (this.equipAllAttrCids == null)
            {
                ListPool<int>.Put(this.equipAllAttrCids);
            }
            this.equipAllAttrCids = null;
            this.ClearData();
        }

        private void ClearData()
        {
            this.heroData = null;
            this.equipItem = null;
            this.columnEquipItem = null;
            this.callOnClickBtn = null;

            this.equipAllAttrCid2Value = null;
            this.equipAllAttrCids?.Clear();
        }

        private void OnClickDetail()
        {
            // 装备 详情
            this.ExcuteCallOnClick(EquipTipBtnType.Detail);
        }

        private void OnClickStrengthen()
        {
            // 装备 强化
            this.ExcuteCallOnClick(EquipTipBtnType.Strengthen);
        }

        private void OnClickTakeOff()
        {
            // 装备 卸下
            this.ExcuteCallOnClick(EquipTipBtnType.TakeOff);
        }

        private void OnClickWear()
        {
            // 装备 穿戴
            this.ExcuteCallOnClick(EquipTipBtnType.Wear);
        }

        private void ExcuteCallOnClick(EquipTipBtnType btnType)
        {
            this.callOnClickBtn?.Invoke(btnType, this.equipItem);
        }

        public void ShowEquipWear(ItemEquipmentData equipItem, HeroData heroData, ItemEquipmentData columnEquipItem = null, System.Action<EquipTipBtnType, ItemEquipmentData> funcBtnClick = null)
        {
            this.callOnClickBtn = funcBtnClick;
            this.heroData = heroData;
            this.columnEquipItem = columnEquipItem;

            if (!this.ShowDefault(equipItem))
            {
                this.ClearData();
                return;
            }
            this.ShowEquipWearBtn();
        }

        private bool ShowDefault(ItemEquipmentData equipItem)
        {
            if (!this.IsValidData(equipItem))
            {
                this.Hide();
                return false;
            }
            this.SetData(equipItem);

            this.Show();
            this.SetUnitDefualt();
            this.ShowBase();
            this.ShowAttribute();
            this.ShowSkill();
            return true;
        }

        private bool IsValidData(ItemData itemData)
        {
            return (itemData != null && itemData.cfgData != null);
        }

        private void SetData(ItemEquipmentData equipItem)
        {
            this.equipItem = equipItem;
            this.equipAllAttrCid2Value = AttributeDataUtils.GetEquipAllAttributeDic(equipItem, GameDataCenter.PlayerData.equipSkins);
            this.equipAllAttrCids.Clear();
            this.equipAllAttrCids.AddRange(this.equipAllAttrCid2Value.Keys);
        }

        private void SetUnitDefualt()
        {
            this.ui.buttonDetail.SetActive(false);
            this.ui.buttonStrengthen.SetActive(false);
            this.ui.buttonTakeOff.SetActive(false);
            this.ui.buttonWear.SetActive(false);
        }

        private void ShowBase()
        {
            ItemEquipmentData itemEquip = columnEquipItem ?? equipItem;
            int currColumn = heroData.GetWearEquipColumn(itemEquip);
            this.item.ShowEquip(equipItem, heroData, currColumn);

            var cfgItem = this.equipItem.cfgData;
            this.ui.bgQuality.SetInt(cfgItem.Quality);
            this.ui.textName.text = cfgItem.Name;
            // this.ui.textNameEng.text = cfgItem.NameEng;
        }

        private void ShowAttribute()
        {
            int cntAttribute = this.equipAllAttrCids.Count;
            this.attrs.RefillCells(cntAttribute);
        }

        private void OnAttrChanged(UIN_CommonAttribute item, int index)
        {
            int attrCid = this.equipAllAttrCids[index];
            int attrValue = this.equipAllAttrCid2Value[attrCid];
            int changNum = AttributeDataUtils.GetEquipChangeAttributeByCompare(this.equipItem, attrCid, this.columnEquipItem);
            item.Show(attrCid, attrValue, changNum);
        }

        private void ShowSkill()
        {
            var skillIds = this.equipItem.cfgStar.SkillIds;
            int cnt = Math.Min(skillIds.Count, this.equipItem.star);
            this.skills.RefillCells(cnt);
        }

        private void OnSkillChanged(UIN_CommonEquipTipsSkill item, int index)
        {
            var skillIds = this.equipItem.cfgStar.SkillIds;
            int skillCid = skillIds[index];
            var cfgSkill = TableCenter.skill.Get(skillCid);
            var cfgSkillLv = TableCenter.skillLevel.GetCfg(skillCid, 1);
            item.Show(1, cfgSkill.Name, cfgSkillLv.EffectText);
        }

        private void ShowEquipWearBtn()
        {
            bool isCanShowBtn = this.callOnClickBtn != null;
            if (!isCanShowBtn)
                return;

            this.ui.buttonStrengthen.SetActive(true);
            bool isCurr = (this.equipItem != this.columnEquipItem);
            this.ui.buttonWear.SetActive(isCurr);
            this.ui.buttonTakeOff.SetActive(!isCurr);
        }
    }
}
